<?php
class Merchant_Model_PlanUsageDetail
{
    protected $_dbTable;
	private $_table;
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_PlanUsageDetail();
	}
	
    public function setDbTable($dbTable){
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('非正确的table类型！');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable(){
        if (null === $this->_dbTable) {
            $this->setDbTable('Merchant_Model_DbTable_PlanUsageDetail');
        }
        return $this->_dbTable;
    }
		
	public function add($row){
		return $this->_dbTable->insert($row);
	}
	
	public  function update($row,$value,$field='pud_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->update($row, $where);

	}

	public function delete($value,$field='pud_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);
		return $this->_table->delete($where);
	}


	public function getByValue($value,$field='pud_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}

	public function getByCond($cond,$type,$order='',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		$select->where('customer_id = ?',$cond['customer_id']);
		if(isset($cond['pu_id']) && $cond['pu_id'] !==''){
			$select->where(' pu_id = ?',$cond['pu_id']);
		}
		if(isset($cond['type']) && $cond['type'] !==''){
			$select->where(' type = ?',$cond['type']);
		}
		if(isset($cond['ref_id']) && $cond['ref_id'] !=''){
			$select->where(' ref_id = ?',$cond['ref_id']);
		}

		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        $select->order($order);
        if ($count==0){        	
        	return $this->_table->fetchAll($select)->toArray(); 
        }		
		$select->limit($count,$offset);
	
		return $this->_table->fetchAll($select)->toArray();
	}
	

}